<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;
use think\Db;
use Yansongda\Pay\Pay;
use app\api\model\Third;
class Order extends Api
{

    protected $noNeedLogin = ['notifyx','returnx'];
    protected $noNeedRight = '*';

    public function listData()
    {
        $param=$this->request->param();
        $where['num']=$param['num'];
        $where['pay_time']=['>',0];
        $list=Db::name('order')->where($where)->order('id desc')->paginate();
        $this->success('',$list);
    }
    public function create()
    {
        $param=$this->request->param();
        $user=$this->auth;
        $data['num']=$param['num'];
        $data['user_id']=$user->id;
        $data['order_sn']=time().rand(1000,9999).$user->id;
        $data['money']=$param['money'];
        $data['createtime']=time();
        $order_id=Db::name('order')->insertGetId($data);
        $this->success('ok',['order_id'=>$order_id]);
    }
    public function pay()
    {
        $param=$this->request->param();
        $user=$this->auth;
        $Third = new Third();
        $where['id']=$param['order_id'];
        $where['user_id']=$user->id;
        $find=Db::name('order')->where($where)->find();
        if(empty($find)){
            $this->error('订单不存在');
        }
        $where_third['user_id']=$user->id;
        $third_find=$Third->get($where_third);
        $notifyurl=request()->root(true).'/api/Order/notifyx/paytype/wechat';
        $returnurl=request()->root(true).'/api/Order/returnx/paytype/wechat';
        $params = [
            'type'         => "wechat",
            'orderid' => $find['order_sn'],
            'title'        => "支付费用",
            'amount'       => $find['money'],
            'method'       => "mini",
            'openid'       => $third_find['openid'],
            'notifyurl'    => $notifyurl,
            'returnurl'    => $returnurl,
        ];
        $result = Service::submitOrder($params);
        $this->success('',$result);
    }
    public function notifyx()
    {
        $type = $this->request->param('paytype');
        $pay = Service::checkNotify($type);
        $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();
        try {
            $out_trade_no = $data['out_trade_no'];
            $where['order_sn']=$out_trade_no;
            $order_find=Db::name('order')->where($where)->find();
            if($order_find['pay_time']==0){
                $Building_find=Db::name('building')->where(['num'=>$order_find['num']])->find();

                $where_find['num']=$order_find['num'];
                if($Building_find['money']+$order_find['money']>0){
                    $update['debt_time']="";
                    Db::name('building')->where($where_find)->update($update);
                }
                Db::name('building')->where($where_find)->setInc('money', $order_find['money']);

                $updata['pay_time']=time();
                Db::name('order')->where($where)->update($updata);
            }

        } catch (Exception $e) {
        }
        //下面这句必须要执行,且在此之前不能有任何输出
        return $pay->success()->send();
    }
}
